# 信号量
import time
import random
from threading import Thread, Semaphore

sm = Semaphore(5)  # 设置5个信号量 类似tcp链接池

def task(i):
    sm.acquire()
    print(f'{i}')
    time.sleep(random.randint(1,5))
    sm.release()


if __name__ == '__main__':
    for i in range(20):
        t = Thread(target=task,args=(i,))
        t.start()